home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / genctxt.zip / CHAP14.TXT < prev    next >
Text File  |  1987-11-21  |  14KB  |  323 lines

  1.  
  2.                      Chapter 14 - Example Programs
  3.  
  4.  
  5.                              WHY THIS CHAPTER?
  6.  
  7.              Although  every  program  in this tutorial has  been  a
  8.         complete  program,  each  one  has also been  a  very  small
  9.         program intended to teach you some principle of  programming
  10.         in  C.   It  would do you a disservice to leave you at  that
  11.         point  without introducing you to a few larger  programs  to
  12.         illustrate  how  to  put together the  constructs  you  have
  13.         learned  to create a major program.   This chapter  contains
  14.         four  programs  of increasing complexity,  each designed  to
  15.         take  you  into a higher plateau of  programming,  and  each
  16.         designed to be useful to you in some way.
  17.  
  18.              DOSEX will illustrate how to make DOS system calls  and
  19.         will teach you,  through self-study, how the system responds
  20.         to  the  keyboard.   WHATNEXT  reads commands input  on  the
  21.         command line and will aid you in setting up a variable batch
  22.         file,  one  that requests an operator input and responds  to
  23.         the  input  by branching to a different part  of  the  batch
  24.         file.
  25.  
  26.              LIST  is  the source code for the program you  used  to
  27.         print  out the C source files when you began studying C with
  28.         the aid of this tutorial.  Finally we come to VC, the Visual
  29.         Calculator,  which  you should find to be a  useful  program
  30.         even  if you don't study its source code.   VC uses most  of
  31.         the  programming  techniques we have studied in this  course
  32.         and  a few that we never even mentioned such  as  separately
  33.         compiled subroutines.
  34.  
  35.              We  will  take a look at the example programs one at  a
  36.         time  but  without  a complete explanation of  any  of  them
  37.         because  you  have  been studying C for some  time  now  and
  38.         should be able to read and understand most of these programs
  39.         on  your  own.
  40.  
  41.                      DOSEX.C - The DOS Example Program
  42.  
  43.              The  copy of DOS that you received with your IBM-PC  or
  44.         compatible has about 80 internal DOS calls that you can  use
  45.         as  a programmer to control your peripheral devices and read
  46.         information  or status from them.   Some of the earlier  IBM
  47.         DOS manuals, DOS 2.0 and earlier, have these calls listed in
  48.         the back of the manual along with how to use them.   Most of
  49.         the  manuals  supplied  with compatible  computers  make  no
  50.         mention  of  these  calls even  though  they  are  extremely
  51.         useful.   These  calls  can  be  accessed  from  nearly  any
  52.         programming  language but they do require some initial study
  53.         to learn how to use them.   This program is intended to  aid
  54.         you in this study.
  55.  
  56.  
  57.  
  58.                                   Page 99
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                      Chapter 14 - Example Programs
  69.  
  70.  
  71.              Display the program on your monitor or print it out for
  72.         reference.   It  is  merely a loop watching for  a  keyboard
  73.         input or a change in the time.  If either happens, it reacts
  74.         accordingly.   In line 32,  the function "kbhit()" returns a
  75.         value  of 1 if a key has been hit but not yet read from  the
  76.         input buffer by the program.
  77.  
  78.              Look at the function named "get_time" for an example of
  79.         a  DOS call.   An interrupt 21(hex) is called after  setting
  80.         the  AH  register to 2C(hex) =  44(decimal).   The  time  is
  81.         returned in the CH,  CL, and DH registers.  Refer to the DOS
  82.         call  definitions in your copy of DOS.   If the  definitions
  83.         are  not included there,  Peter Nortons  book,  "Programmers
  84.         Guide  to  the  IBM PC" is recommended as a  good  reference
  85.         manual   for   these  calls  and  many   other   programming
  86.         techniques.   Your compiler may have a built in function  to
  87.         do this.  If you read your documentation, you will  probably
  88.         find many useful functions available with your compiler that
  89.         are  included  as  a convenience for you  by  your  compiler
  90.         writer.
  91.  
  92.              Another useful function is the "pos_cursor()"  function
  93.         that  positions the cursor anywhere on the monitor that  you
  94.         desire  by  using  a  DOS  interrupt.   In  this  case,  the
  95.         interrupt  used  is  10(hex) which is  the  general  monitor
  96.         interrupt.  This particular service is number 2 of about  10
  97.         different  monitor  services available.   This  function  is
  98.         included here as another example to you.
  99.  
  100.              The  next  function,  service  number  6  of  interrupt
  101.         10(hex)  is the window scroll service.   It should  be  self
  102.         explanatory.
  103.  
  104.              In this program, the cursor is positioned and some data
  105.         is  output  to the monitor,  then the cursor is "hidden"  by
  106.         moving  it  to line 26 which is not  displayed.   After  you
  107.         compile and run the program, you will notice that the cursor
  108.         is  not  visible on the monitor.   This is possible  in  any
  109.         program,  but  be  sure  to put the cursor  in  view  before
  110.         returning  to  DOS  because  DOS does not  like  to  have  a
  111.         "hidden" cursor and may do some strange things.
  112.  
  113.              Some time spent studying this program will be  valuable
  114.         to  you as it will reveal how the keyboard data is input  to
  115.         the  computer.   Especially of importance is how the special
  116.         keys such as function keys, arrows, etc. are handled.   Also
  117.         note that this program uses full prototype checking and is a
  118.         good  example  of  how to use it.  Since it  also  uses  the
  119.         "modern"  method  of  function definitions,  it  is  a  good
  120.         example of that also.
  121.  
  122.  
  123.  
  124.                                   Page 100
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                      Chapter 14 - Example Programs
  135.  
  136.  
  137.  
  138.                   WHATNEXT.C - The Batch File Interrogator
  139.  
  140.              This  is  an  example of how to read the  data  on  the
  141.         command line following the function call.  Notice that there
  142.         are  two variables listed within the  parentheses  following
  143.         the main() call.   The first variable is a count of words in
  144.         the entire command line including the command itself and the
  145.         second  variable  is  a  pointer to  an  array  of  pointers
  146.         defining the actual words on the command line.
  147.  
  148.              First the question on the command line, made up of some
  149.         number of words, is displayed on the monitor and the program
  150.         waits for the operator to hit a key.   If the key hit is one
  151.         of  those  in the last "word" of the group of words  on  the
  152.         command  line,  the number of the character within the group
  153.         is  returned to the program where it can be tested with  the
  154.         "errorlevel" command in the batch file.   You could use this
  155.         technique  to  create a variable AUTOEXEC.BAT  file  or  any
  156.         other  batch  file  can  use this for  a  many  way  branch.
  157.         Compile  and  run this file with TEST.BAT for an example  of
  158.         how  it  works in practice.   You may  find  this  technique
  159.         useful  in  one  of  your batch files and  you  will  almost
  160.         certainly  need  to  read in  the  command  line  parameters
  161.         someday.
  162.  
  163.              An  interesting alternative would be for you to write a
  164.         program  named "WOULD.C" that would return a 1 if a  "Y"  or
  165.         "y"  were typed and a zero if any other key were hit.   Then
  166.         your batch file could have a line such as;
  167.  
  168.         WOULD YOU LIKE TO USE THE ALTERNATIVE METHOD (Y/N)
  169.  
  170.              Dos would use "WOULD" as the program name,  ignore  the
  171.         rest  of  the  statement  except for displaying  it  on  the
  172.         screen.   You  would  then respond to the  question  on  the
  173.         monitor  with a single keyhit.   Your batch file would  then